001    package net.sf.xdc;
002    
003    /*
004     *  Copyright 2005-2006 Jens Voß.
005     *
006     *  Licensed under the GNU Lesser General Public License (the "License");
007     *  you may not use this file except in compliance with the License.
008     *  You may obtain a copy of the License at
009     *
010     *       http://opensource.org/licenses/lgpl-license.php
011     *
012     *  Unless required by applicable law or agreed to in writing, software
013     *  distributed under the License is distributed on an "AS IS" BASIS,
014     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     *  See the License for the specific language governing permissions and
016     *  limitations under the License.
017     */
018    
019    import java.util.List;
020    import java.util.Vector;
021    import java.util.Arrays;
022    import java.io.File;
023    import java.io.IOException;
024    
025    import org.apache.commons.cli.BasicParser;
026    import org.apache.commons.cli.Options;
027    import org.apache.log4j.Logger;
028    import net.sf.xdc.util.FileTokenizer;
029    import net.sf.xdc.util.Logging;
030    
031    /**
032     * This class provides a command line parser which mainly differs from its
033     * parent, {@link BasicParser}, in the fact that it resolves paths to files
034     * containing additional command line options ('@' notation).
035     *
036     * @author Jens Voß
037     * @since 0.5
038     * @version 0.5
039     */
040    public class XdcParser extends BasicParser {
041    
042      private static final Logger LOG = Logging.getLogger();
043    
044      protected String[] flatten(Options options, String[] arguments,
045                                 boolean stopAtNonOption) {
046        List retVal = new Vector();
047        for (int i = 0; i < arguments.length; i++) {
048          String arg = arguments[i];
049          if (arg.length() == 0 || arg.charAt(0) != '@') {
050            retVal.add(arg);
051          }
052          else {
053            try {
054              FileTokenizer tok = new FileTokenizer(new File(arg.substring(1)));
055              retVal.addAll(Arrays.asList(tok.getContents()));
056            }
057            catch (IOException e) {
058              LOG.error(e.getMessage(), e);
059            }
060          }
061        }
062        return (String[]) retVal.toArray(new String[retVal.size()]);
063      }
064    
065    }